const db = require("../../../db");

const login = (req, res) => {
  let { account, password, type } = req.body;
  console.log(account, password, type);
  let sql = `select * from user where `;
  if (type === "qq") {
    sql += `qq='${account}'`;
  } else if (type === "email") {
    sql += `email='${account}'`;
  } else {
    sql += `phoneNumber='${account}'`;
  }
  db.query(sql, (err, result) => {
    if (err) return res.send({ code: 2, message: err.message });
    if (result.length === 0)
      return res.send({ code: 1, message: "该登录账号不存在" });
    sql = `select * from user where password='${password}' and `;
    if (type === "qq") {
      sql += `qq='${account}'`;
    } else if (type === "email") {
      sql += `email='${account}'`;
    } else {
      sql += `phoneNumber='${account}'`;
    }
    db.query(sql, (err, result) => {
      if (err) return res.send({ code: 2, message: err.message });
      if (result.length === 0) {
        return res.send({ code: 1, message: "账号或密码错误" });
      }
      res.send({ code: 0, message: "登录成功", data: result[0] });
    });
  });
};

module.exports = login;
